package com.ruoyi.erecord.employee.controller;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erecord.employee.domain.bo.EmployeeImportBaseBo;
import com.ruoyi.erecord.employee.domain.vo.EmployeeImportBaseVo;
import com.ruoyi.erecord.employee.domain.vo.EmployeeVo;
import com.ruoyi.erecord.employee.service.IEmployeeImportBaseService;
import com.ruoyi.erecord.employee.service.impl.EmployeeImportExcelService;
import com.ruoyi.system.domain.vo.SysUserImportVo;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人员导入主
 *
 * @author cc
 * @date 2023-04-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erecord/employeeImportBase")
public class EmployeeImportBaseController extends BaseController {

    private final IEmployeeImportBaseService iEmployeeImportBaseService;
    private final EmployeeImportExcelService employeeImportExcelService;
    /**
     * 查询人员导入主列表
     */
    @SaCheckPermission("erecord:employeeImportBase:list")
    @GetMapping("/list")
    public TableDataInfo<EmployeeImportBaseVo> list(EmployeeImportBaseBo bo, PageQuery pageQuery) {
        return iEmployeeImportBaseService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出人员导入主列表
     */
    @SaCheckPermission("erecord:employeeImportBase:export")
    @Log(title = "人员导入主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EmployeeImportBaseBo bo, HttpServletResponse response) {
        List<EmployeeImportBaseVo> list = iEmployeeImportBaseService.queryList(bo);
        ExcelUtil.exportExcel(list, "人员导入主", EmployeeImportBaseVo.class, response);
    }

    /**
     * 获取人员导入主详细信息
     *
     * @param batchNo 主键
     */
    @SaCheckPermission("erecord:employeeImportBase:query")
    @GetMapping("/{batchNo}")
    public R<EmployeeImportBaseVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String batchNo) {
        return R.ok(iEmployeeImportBaseService.queryById(batchNo));
    }

    /**
     * 新增人员导入主
     */
    @SaCheckPermission("erecord:employeeImportBase:add")
    @Log(title = "人员导入主", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EmployeeImportBaseBo bo) {
        return toAjax(iEmployeeImportBaseService.insertByBo(bo));
    }

    /**
     * 修改人员导入主
     */
    @SaCheckPermission("erecord:employeeImportBase:edit")
    @Log(title = "人员导入主", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EmployeeImportBaseBo bo) {
        return toAjax(iEmployeeImportBaseService.updateByBo(bo));
    }

    /**
     * 删除人员导入主
     *
     * @param batchNos 主键串
     */
    @SaCheckPermission("erecord:employeeImportBase:remove")
    @Log(title = "人员导入主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{batchNos}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] batchNos) {
        return toAjax(iEmployeeImportBaseService.deleteWithValidByIds(Arrays.asList(batchNos), true));
    }
    
    /**
     * 新增人员导入主
     */
    @SaCheckPermission("erecord:employeeImportBase:import")
    @Log(title = "人员导入主", businessType = BusinessType.IMPORT)
    @RepeatSubmit()
    @PostMapping("/importData")
    public R<Void> importData(@Validated(AddGroup.class) @RequestBody EmployeeImportBaseBo bo) {
        return toAjax(this.employeeImportExcelService.importExcel(bo));
    }
    
    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "人员数据", EmployeeVo.class, response);
    }
}
